//
//  ONObjectRealTime.m
//  Volatix
//
//  Created by Familly on 6/13/14.
//  Copyright (c) 2014 Familly. All rights reserved.
//

#import "ONObjectRealTime.h"
#import "ONColumnTitleObject.h"
@interface ONObjectRealTime ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end
@implementation ONObjectRealTime
+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict
{
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict
{
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    BOOL isParsing=NO;
    if(self && [dict isKindOfClass:[NSDictionary class]]) {
        /// bool
        isParsing=YES;
         self.isDailyHighIncreased = [[self objectOrNilForKey:@"isDailyHighIncreased" fromDictionary:dict] boolValue];
         self.isDailyLowIncreased = [[self objectOrNilForKey:@"isDailyLowIncreased" fromDictionary:dict] boolValue];
         self.isHourlyHighIncreased = [[self objectOrNilForKey:@"isHourlyHighIncreased" fromDictionary:dict] boolValue];
         self.isHourlyLowIncreased = [[self objectOrNilForKey:@"isHourlyLowIncreased" fromDictionary:dict] boolValue];
         self.isMonthlyHighIncreased = [[self objectOrNilForKey:@"isMonthlyHighIncreased" fromDictionary:dict] boolValue];
         self.isMonthlyLowIncreased = [[self objectOrNilForKey:@"isMonthlyLowIncreased" fromDictionary:dict] boolValue];
         self.isWeeklyHighIncreased = [[self objectOrNilForKey:@"isWeeklyHighIncreased" fromDictionary:dict] boolValue];
         self.isWeeklyLowIncreased = [[self objectOrNilForKey:@"isWeeklyLowIncreased" fromDictionary:dict] boolValue];
        /// float
        self.dailyHigh = [[self objectOrNilForKey:@"dailyHigh" fromDictionary:dict] floatValue];
        self.dailyLow = [[self objectOrNilForKey:@"dailyLow" fromDictionary:dict] floatValue];
        self.hourlyHigh = [[self objectOrNilForKey:@"hourlyHigh" fromDictionary:dict] floatValue];
        self.hourlyLow = [[self objectOrNilForKey:@"hourlyLow" fromDictionary:dict] floatValue];
        self.monthlyHigh = [[self objectOrNilForKey:@"monthlyHigh" fromDictionary:dict] floatValue];
        self.monthlyLow = [[self objectOrNilForKey:@"monthlyLow" fromDictionary:dict] floatValue];
        self.weeklyHigh = [[self objectOrNilForKey:@"weeklyHigh" fromDictionary:dict] floatValue];
        self.weeklyLow = [[self objectOrNilForKey:@"weeklyLow" fromDictionary:dict] floatValue];
        
        NSString *strDate=[Utils objectForKeyNotNull:@"priceDate" andDictionnary:dict];
        self.priceDate = [Utils getDateTimeAtZeroTime:strDate];
        
        //// Current
        self.isCurrentIncreased = [[self objectOrNilForKey:@"isCurrentIncreased" fromDictionary:dict] boolValue];
        self.current = [[self objectOrNilForKey:@"current" fromDictionary:dict] floatValue];
        
        /// Name
        
        self.ISO=[self objectOrNilForKey:@"iso" fromDictionary:dict];
        self.LOADZONE=[self objectOrNilForKey:@"loadZone" fromDictionary:dict];
        self.POWERPOINT=[self objectOrNilForKey:@"powerpoint" fromDictionary:dict];
        
        
        
        // Column Dynamic
        
        NSMutableArray * arrayColumn= [APP_DELEGATE getingListDynamicColumnNameRealTime:YES];
        if(_dynamicColumnData!=nil)
        {
            [_dynamicColumnData removeAllObjects];
        }
        else
        {
            _dynamicColumnData = [[NSMutableArray alloc] init];
        }
        for (int index=0; index<[arrayColumn count]; index++) {
            ///
            ONColumnTitleObject *keyColumn = [arrayColumn objectAtIndex:index];
            ONDataDynamicColumn * item = [[ONDataDynamicColumn alloc] init];
            item.nameColumn = keyColumn.name;
            if(item.nameColumn!=nil)
            {
               // NSLog(@"%@" ,[NSString stringWithFormat:@"is%@Increased",item.nameColumn]);
                item.Value=[[self objectOrNilForKey:item.nameColumn fromDictionary:dict] floatValue];
                item.isInscrease=[[self objectOrNilForKey:[NSString stringWithFormat:@"is%@Increased",item.nameColumn] fromDictionary:dict] boolValue];
            }
            if(item.Value!=0)
            {
            
                [_dynamicColumnData addObject:item];
            }
            
        }
        
        
    }
    
    return (isParsing)?self:nil;
    
}
#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
{
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


@end
